package com.neuedu.xkxt.service.impl;


import com.neuedu.xkxt.dao.CourseDao;
import com.neuedu.xkxt.dao.StudentDao;
import com.neuedu.xkxt.dao.TeacherDao;
import com.neuedu.xkxt.dao.TeacherMapper;
import com.neuedu.xkxt.model.Student;
import com.neuedu.xkxt.model.StudentExample;
import com.neuedu.xkxt.model.Teacher;
import com.neuedu.xkxt.model.TeacherExample;
import com.neuedu.xkxt.service.UserService;
import com.neuedu.xkxt.vo.CourseVO;
import com.neuedu.xkxt.vo.PageVO;
import org.apache.ibatis.annotations.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
@Resource
    private TeacherDao teacherDao;
@Resource
    private StudentDao studentDao;
@Resource
    private TeacherMapper teacherMapper;

    @Override
    public Teacher findUserByTeacherName(String name) {
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andNameEqualTo(name);
        List<Teacher> teachers = teacherDao.selectByExample(teacherExample);
        if (teachers!=null&&teachers.size() > 0) {
            return teachers.get(0);
        }
        return null;
    }

    @Override
    public Student findUserByStudentName(String name) {
        StudentExample studentExample = new StudentExample();
        studentExample.createCriteria().andNameEqualTo(name);
        List<Student> students = studentDao.selectByExample(studentExample);
        if (students!=null&&students.size() > 0) {
            return students.get(0);
        }
        return null;
    }
    @Override
    public int addStudent(Student student) {

        return studentDao.insertSelective(student);
    }

    @Override
    public PageVO<CourseVO> findPage(String searchKey, int pageNo, int pageSize) {
        searchKey="%"+searchKey+"%";
        List<CourseVO> data=teacherMapper.findCourseByPage(searchKey,(pageNo-1)*pageSize,pageSize);
        int count=teacherMapper.countByPage(searchKey);

        PageVO<CourseVO> pageVO=new PageVO<>();
        pageVO.setData(data);
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setPageCount(count/pageSize+(count%pageSize==0?0:1));

        return pageVO;
    }
}
